package com.xiashitech.apiservice.plugin;

import com.xiashitech.apiservice.plugin.base.BaseGenerator;
import com.xiashitech.interfaces.authservice.AuthService;
import com.xiashitech.interfaces.sqlparser.SqlParser;
import com.xiashitech.apiservice.service.IEnAndDecryption;
import com.xiashitech.apiservice.util.EnAndDecryptionHolder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Component
public class ServiceGenerator extends BaseGenerator {

    @Bean
    public SqlParser getSqlParserService() {
        List<SqlParser> sqlParsers = getService(SqlParser.class);
        if(sqlParsers.size() > 0) {
            return sqlParsers.get(0);
        } else {
            return null;
        }
    }

    @Bean
    public AuthService getAuthService() {
        List<AuthService> authServices = getService(AuthService.class);
        if(authServices.size() > 0) {
            return authServices.get(0);
        } else {
            return null;
        }
    }

    @Bean
    public EnAndDecryptionHolder getCryptionHolder() {
        List<IEnAndDecryption> authServices = getService(IEnAndDecryption.class);
        if(authServices.size() > 0) {
            EnAndDecryptionHolder enAndDecryptionHolder = EnAndDecryptionHolder.builder().enAndDecryptionMap(new HashMap<String,IEnAndDecryption>()).build();
            authServices.stream().forEach(authService ->enAndDecryptionHolder.getEnAndDecryptionMap().put(authService.cryptionType(),authService));
            return enAndDecryptionHolder;
        } else {
            return null;
        }
    }

}
